package cn.e4j.modular.crm.controller;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.LangUtils;
import cn.e4j.modular.crm.entity.Gwai810Export;
import cn.e4j.modular.crm.service.impl.GwdbTServiceImpl;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 商品价格查询
 * Created by miaoyl on 2019-1-10.
 */
@Controller
@RequestMapping("/crm/gwai810")
public class Gwai810Controller extends BaseController {

    @Autowired
    private GwdbTServiceImpl gwdbTService;

    /**
     * 跳转商品价格查询管理页面
     */
    @RequiresPermissions("crm:gwai810:init")
    @RequestMapping(value = {"", "init"})
    public String index() {
        return "/pc/crm/gwai810.html";
    }

    /**
     * 查询商品价格列表
     */
    @RequestMapping("/getListData")
    @RequiresPermissions("crm:gwai810:init")
    @ResponseBody
    public ResponseData getListData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdbTService.listGwam(page, inDto);
        return new PageReData(page, result);
    }

    @RequestMapping(value = "/exportsData", method = RequestMethod.GET)
    @RequiresPermissions("crm:gwai250:init")
    public void exportsData(ModelMap map, HttpServletRequest request, HttpServletResponse response, @DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        ShiroUser user = ShiroKit.getUser();
        inDto.put("lang", LangUtils.getLang(user.getLANG()));
        inDto.put("GWAHENT", user.getDBC());
        List<Map<String, Object>> list = gwdbTService.listGwam(null, inDto);
        JSONArray json_arr = new JSONArray();
        for (Map<String, Object> map1 : list) {
            JSONObject json_obj = new JSONObject();
            for (Map.Entry<String, Object> entry : map1.entrySet()) {
                String key = entry.getKey();
                Object value = entry.getValue();
                json_obj.put(key, value);
            }
            json_arr.add(json_obj);
        }
        List<Gwai810Export> gwai810Exports = new ArrayList<>();
        gwai810Exports = JSONObject.parseArray(json_arr.toString(),Gwai810Export.class);
        ExportParams params = new ExportParams(null, "商品价格查询", ExcelType.XSSF);
        //params.setFreezeCol(2);
        map.put(NormalExcelConstants.FILE_NAME, "商品价格查询");
        map.put(NormalExcelConstants.DATA_LIST, gwai810Exports);
        map.put(NormalExcelConstants.CLASS, Gwai810Export.class);
        map.put(NormalExcelConstants.PARAMS, params);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }
}
